﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FRW.Common.Extensions
{
    public static class StringExtension
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="toCheck"></param>
        /// <param name="comp"></param>
        /// <returns></returns>
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }

        public static string Truncate(this string text, int length = 20)
        {
            string newText = text;

            if (text.Length > length)
            {
                newText = text.Substring(0, length - 3) + "...";
            }

            return newText;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string MD5(this string inputString)
        {
            byte[] textBytes = System.Text.Encoding.Default.GetBytes(inputString);
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
                cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash = cryptHandler.ComputeHash(textBytes);
                string ret = "";
                foreach (byte a in hash)
                {
                    ret += a.ToString("x2");
                }
                return ret;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// This method calculates what the divider should be to get the supplied number to a manageable display size. It
        /// also counts how many divides it takes to achieve this.
        /// </summary>
        /// <param name="size">Size to calculate the parameters for</param>
        /// <param name="count">Output parameter of how many divides it took to divide down the supplied size</param>
        /// <returns></returns>
        private static int CalculateDivider(Int64 size, out int count)
        {
            int divider = 1;
            Int64 tmpTotal = size;
            count = 0;

            while (tmpTotal > 1024)
            {
                count++;
                divider *= 1024;
                tmpTotal /= 1024;
            }


            return divider;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string ToDiskSize(this long size)
        {
            string[] sizeText = { "B", "KB", "MB", "GB", "TB", "PB" };
            String sizeTextVal;

            int units;
            int divider = CalculateDivider(size, out units);

            if (size / divider < 10)
            {
                float decSize = (float)size / divider;
                sizeTextVal = decSize.ToString("F2");
            }
            else
            {
                size /= divider;
                sizeTextVal = size.ToString();
            }

            return sizeTextVal + sizeText[units]; ;
        }
    }
}
